﻿; (function (window, undefined, $) {
    var Request = new Object();
    Request = GetRequest();
    $(document).ready(function () {
        var gridopt = {
            url: options.listUrl + "?id=" + Request["id"]+"&projectid="+Request["pid"],
            colModel: [
                    { display: '项目编号', name: 'Id', width: "5%", sortable: true, hide: false, align: 'left', iskey: true },
                    { display: '项目名称', name: 'ProjectName', width: "15%", sortable: false, align: 'left' },
                    { display: '投资人', name: 'Name', width: "8%", sortable: false, align: 'left' },
                    { display: '投资金额', name: 'TenderAmt', width: "15%", sortable: false, align: 'left', process:processTotalLoan },
                    { display: '年化收益率', name: 'AnnualisedRates', width: "12%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '活动受增', name: 'AddAnnualisedRates', width: "12%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '会员受增', name: 'AddMAnnualisedRates', width: "12%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '当期收益', name: 'Profit', width: "12%", sortable: false, align: 'left', process: processTotalLoan }
            ],
            sortname: "ID",
            sortorder: "ASC",
            title: false,
            rp: 15,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridlist", gridopt);
        
        util.RepayPres = function (id,pid) {
            $.post(options.Repayment, { id: id, projectid: pid, name: $('#RepaymentUser').val() },
                  function (res) {
                      if (res > 0) {
                          if (res == 10001) {
                              alert('还没到还款时间！');
                          }
                          else if (res == 10003) {
                              alert('已还款！');
                          }
                          else {
                              window.location.href = "../Admin/Project/RepaymentList";
                          }
                      }
                      else {
                          alert("操作失败！");
                      }
                  },
                  "json"
            );
        };

    });

    function processAnnualisedRates(value) {
        var ops = [];
        ops.push(Math.round(value * 100), "%");
        return ops.join("");
    }

    function processTotalLoan(value) {
        return formatNum(value);
    }

    function formatNum(v) {
        if (isNaN(v)) {
            return v;
        }
        v = (Math.round((v - 0) * 100)) / 100;
        v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v
                + "0" : v);
        v = String(v);
        var ps = v.split('.');
        var whole = ps[0];
        var sub = ps[1] ? '.' + ps[1] : '.00';
        var r = /(\d+)(\d{3})/;
        while (r.test(whole)) {
            whole = whole.replace(r, '$1' + ',' + '$2');
        }
        v = whole + sub;

        return v;
    }
    //这里就是调用
    $("#btnRept").click(function () {
        var sign = "merchantNo=" + $("#YongHuHuanKuanmerchantNo").val() + "&timestamp=" + $("#YongHuHuanKuantimestamp").val()
            + "&userName=" + $("#YongHuHuanKuanuserName").val() + "&origOrderType=" + $("#YongHuHuanKuanorigOrderType").val()
            + "&issueBidNo=" + $("#YongHuHuanKuanissueBidNo").val() + "&repaymentNo=" + $("#YongHuHuanrepaymentNo").val()
            + "&repaymentType=" + $("#YongHuHuanrepaymentType").val() + "&repaymentAmount=" + $("#YongHuHuanrepaymentAmount").val()
            + "&repaymentMode=" + $("#YongHuHuanrepaymentMode").val() + "&isReward=" + $("#YongHuHuanisReward").val()
            + "&transferItems=" + $("#YongHuHuantransferItems").val() + "&callback=" + $("#YongHuHuancallback").val()
            + "&notifyUrl=" + $("#YongHuHuannotifyUrl").val();
        sign = sign.replace(/\&/g, "|");
        $.ajax({
            url: "/T_Project/WebRepayment",
            type: "Post",
            success: function(data) {
                var time = data.substring(0, 14);
                var sign2 = data.substring(14, data.length);
                $("#YongHuHuanKuantimestamp").val(time);
                $("#YongHuHuanKuansign").val(sign2);
                $("#YongHuHuanKuanForm").submit();
            },
            data: "sign="+sign,
            //data: "repaymentPlanId=" + Request["id"] + "&projectId=" + Request["pid"],
        });
    });
    


})(window, undefined, jQuery);

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

